import com.boost.util.ByteUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.MessageToMessageDecoder;

import java.util.List;

/**
 * @author Santa
 * @since 2020/3/23
 */
public class THexDecoder extends ByteToMessageDecoder {
    /**
     * Decode from one message to an other. This method will be called for each written message that can be handled
     * by this encoder.
     *
     * @param ctx the {@link ChannelHandlerContext} which this {@link MessageToMessageDecoder} belongs to
     * @param msg the message to decode to an other one
     * @param out the {@link List} to which decoded messages should be added
     * @throws Exception is thrown if an error occurs
     */
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {

        //int i = ;
        System.out.println("收到数据");
        byte[] array = new byte[msg.readableBytes()];
        msg.readBytes(array);
        String str = ByteUtils.bytesToHexString(array);
        System.out.println("数据转换成str:"+str);
        out.add(str);
    }
}























